package com.karaoke.android.kege.adapter

import android.databinding.ViewDataBinding
import android.view.View
import com.daimajia.swipe.SwipeLayout
import com.daimajia.swipe.implments.SwipeItemAdapterMangerImpl
import com.daimajia.swipe.interfaces.SwipeAdapterInterface
import com.daimajia.swipe.interfaces.SwipeItemMangerInterface
import com.daimajia.swipe.util.Attributes
import com.karaoke.android.data.LoadMoreEvent
import com.karaoke.android.data.domain.PlayList
import com.karaoke.android.data.onUI
import com.karaoke.android.kege.R
import com.karaoke.android.kege.databinding.BangItemBinding
import com.karaoke.android.library.databind.BindingListViewAdapter
import org.simple.eventbus.EventBus
import org.simple.eventbus.Subscriber
import rx.Observable
import java.util.concurrent.TimeUnit

/**
 * Created by Allen on 2016/10/14.
 */
open class SwipeGridBindingAdapter<T> : BindingListViewAdapter<T>(1), SwipeItemMangerInterface, SwipeAdapterInterface
{

    init
    {
        EventBus.getDefault().register(this)
    }


    @Subscriber
    fun onMediaListChanged(mediaList: PlayList)
    {
        mItemManger.closeAllItems()
        Observable.timer(350, TimeUnit.MILLISECONDS).onUI { notifyDataSetChanged() }
    }

    protected var mItemManger = SwipeItemAdapterMangerImpl(this)

    /**
     * return the [com.daimajia.swipe.SwipeLayout] resource id, int the view item.
     * @param position
     * *
     * @return
     */
    override fun getSwipeLayoutResourceId(position: Int): Int
    {
        return R.id.swipe
    }

    override fun openItem(position: Int)
    {
        mItemManger.openItem(position)
    }

    override fun closeItem(position: Int)
    {
        mItemManger.closeItem(position)
    }

    override fun closeAllExcept(layout: SwipeLayout)
    {
        mItemManger.closeAllExcept(layout)
    }

    override fun closeAllItems()
    {
        mItemManger.closeAllItems()
    }

    override fun getOpenItems(): List<Int>
    {
        return mItemManger.openItems
    }

    override fun getOpenLayouts(): List<SwipeLayout>
    {
        return mItemManger.openLayouts
    }

    override fun removeShownLayouts(layout: SwipeLayout)
    {
        mItemManger.removeShownLayouts(layout)
    }

    override fun isOpen(position: Int): Boolean
    {
        return mItemManger.isOpen(position)
    }

    override fun getMode(): Attributes.Mode
    {
        return mItemManger.mode
    }

    override fun setMode(mode: Attributes.Mode)
    {
        mItemManger.mode = mode
    }

    override fun onBindBinding(binding: ViewDataBinding?, bindingVariable: Int, layoutRes: Int, position: Int, item: T)
    {
        super.onBindBinding(binding, bindingVariable, layoutRes, position, item)
        mItemManger.updateConvertView(binding?.root, position)
        if (count - position <= 1)
        {
            EventBus.getDefault().post(LoadMoreEvent())
        }

        if (binding is BangItemBinding)
        {
            binding.position = "NO.${position + 1}"
        }
    }

    override fun onBindingCreated(view: View?, position: Int)
    {
        super.onBindingCreated(view, position)
        mItemManger.initialize(view, position)
    }
}